﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace GUI.BaoCao.BCBanHangTheoHoaDon
{
    public partial class frmBanHangTheoHoaDon : Form
    {
        public frmBanHangTheoHoaDon()
        {
            InitializeComponent();
            try
            {
                try
                {
                    Entities.CaiDatKho kho = Entitystatic.ViewFile();
                    makho = kho.MaKho;
                    tenkho = kho.TenKho;
                    if (makho.Length == 0 || makho == "NULL")
                    {
                        makho = tenkho = "";
                    }
                }
                catch
                {
                }
                date = Entitystatic.date;
                ketthuc = batdau = date;
                SelectData();
                loaihoadon = cbbLoai.Text;
                cbbKho.Text = tenkho;
                lblTenNhanVien.Text = UtilitiesVariable.User.TenDangNhap;

                savefile = new System.Windows.Forms.SaveFileDialog();
            }
            catch
            {
            }
        }

        #region khởi tạo biến
        bool checkClose = false;
        DateTime date;
        DateTime datesosanh;
        DateTime batdau;
        DateTime ketthuc;
        Entities.HDBanBuon[] hdbanbuon;
        Entities.HDBanLe[] hdbanle;
        Entities.ChiTietHDBanBuon[] chitiethdbanbuon;
        Entities.ChiTietHDBanle[] chitiethdbanle;
        Entities.KhoHang[] khohang;
        Entities.GoiHang[] goihang;
        ArrayList array;
        ArrayList array1;
        string makho = "";
        string tenkho = "";
        string loaihoadon = "";
        Entities.BCBanHangTheoHoaDon[] hienthi;
        Entities.BCBanHangTheoHoaDon[] baocao;
        Entities.BCBanHangTheoHoaDon save;
        System.Windows.Forms.SaveFileDialog savefile;
        int i = 0;
        double sotiendathu = 0;
        double sotienphaithu = 0;
        double tongtien = 0;
        double thue = 0;
        double chietkhau = 0;
        int sotang = 0;
        #endregion

        void SelectData()
        {
            hdbanbuon = Common.StaticVariable.InterfaceObject.SelectHDBanBuon();
            if (hdbanbuon == null)
                hdbanbuon = new Entities.HDBanBuon[0];
            chitiethdbanbuon = Common.StaticVariable.InterfaceObject.SelectChiTietHDBanBuon();
            if (chitiethdbanbuon == null)
                chitiethdbanbuon = new Entities.ChiTietHDBanBuon[0];
            hdbanle = Common.StaticVariable.InterfaceObject.SelectHDBanLe();
            if (hdbanle == null)
                hdbanle = new Entities.HDBanLe[0];
            chitiethdbanle = Common.StaticVariable.InterfaceObject.SelectChiTietHDBanLe();
            if (chitiethdbanle == null)
                chitiethdbanle = new Entities.ChiTietHDBanle[0];
            Entities.KhoHang kh = new Entities.KhoHang();
            kh.HanhDong = "1";
            kh.MaKho = "";
            khohang = Common.StaticVariable.InterfaceObject.SelectKhoHang(kh);
            if (khohang == null)
                khohang = new Entities.KhoHang[0];

            Entities.KhoHang[] temp = new Entities.KhoHang[khohang.Length + 1];
            if (temp.Length != 1)
            {
                int sotang = 0;
                foreach (Entities.KhoHang item in khohang)
                {
                    temp[sotang] = item;
                    sotang++;
                }
                temp[temp.Length - 1] = new Entities.KhoHang();
                temp[temp.Length - 1].MaKho = "Tất cả";
                temp[temp.Length - 1].TenKho = "Tất cả";
                Common.Utilities.XuLyComboBox(cbbKho, temp, Common.Constants.KhoHang.TenKho, Common.Constants.KhoHang.MaKho);
            }
            else
            {
                Common.Utilities.XuLyComboBox(cbbKho, khohang, Common.Constants.KhoHang.TenKho, Common.Constants.KhoHang.MaKho);
            }


        }

        #region In
        void In()
        {
            if (hienthi.Length - 1 == 0)
            {
                return;
            }

            try
            {
                Entities.BCBanHangTheoHoaDon[] temp = new Entities.BCBanHangTheoHoaDon[hienthi.Length - 1];
                int sotang = 0;
                foreach (Entities.BCBanHangTheoHoaDon item in hienthi)
                {
                    if (item.MaHD != null)
                    {
                        temp[sotang] = item;
                        sotang++;
                    }
                }
                BaoCao.frmBaoCaorpt frm = new frmBaoCaorpt(temp, tenkho, loaihoadon, batdau, ketthuc);
                if (frm.kiemtra)
                {
                    frm.ShowDialog();
                }
            }
            catch
            {
            }
        }
        #endregion

        #region xử lý
        void XuLy()
        {
            if (makho == "")
            {
                return;
            }
            if (cbbLoai.SelectedIndex == 2)
            {
                XuLyTatCa();
            }
            if (cbbLoai.SelectedIndex == 1)
            {
                XuLyBanBuon();
            }
            if (cbbLoai.SelectedIndex == 0)
            {
                XuLyBanLe();
            }
        }
        #endregion

        #region tất cả
        void XuLyTatCa()
        {
            sotiendathu = sotienphaithu = tongtien = 0;
            sotang = 0;
            array = new ArrayList();
            array1 = new ArrayList();
            foreach (Entities.HDBanLe item in hdbanle)
            {
                if (cbbKho.SelectedIndex == khohang.Length)
                {
                    if (item.NgayLap.Date >= batdau.Date && item.NgayLap.Date <= ketthuc)
                    {
                        XuLy(item);
                    }
                }
                else
                {
                    if (item.MaKho == makho && item.NgayLap.Date >= batdau.Date && item.NgayLap.Date <= ketthuc)
                    {
                        XuLy(item);
                    }
                }
            }

            foreach (Entities.HDBanBuon item in hdbanbuon)
            {
                if (cbbKho.SelectedIndex == khohang.Length)
                {
                    if (item.NgayLap.Date >= batdau.Date && item.NgayLap.Date <= ketthuc)
                    {
                        XuLy(item);
                    }
                }
                else
                {
                    if (item.MaKho == makho && item.NgayLap.Date >= batdau.Date && item.NgayLap.Date <= ketthuc)
                    {
                        XuLy(item);
                    }
                }
            }

            hienthi = new Entities.BCBanHangTheoHoaDon[array1.Count + 1];
            if (array1.Count != 0)
            {
                sotang = 0;
                Entities.BCBanHangTheoHoaDon[] te = (Entities.BCBanHangTheoHoaDon[])array1.ToArray(typeof(Entities.BCBanHangTheoHoaDon));
                foreach (Entities.BCBanHangTheoHoaDon item in te)
                {
                    hienthi[sotang] = item;
                    sotang++;
                }
            }
            hienthi[hienthi.Length - 1] = new Entities.BCBanHangTheoHoaDon();
            hienthi[hienthi.Length - 1].LoaiHoaDon = "Tổng tiền";
            hienthi[hienthi.Length - 1].GiaTriTT = Common.Utilities.FormatMoney(tongtien.ToString());
            hienthi[hienthi.Length - 1].GiaTriDT = Common.Utilities.FormatMoney(sotiendathu.ToString());
            hienthi[hienthi.Length - 1].GiaTriPT = Common.Utilities.FormatMoney(sotienphaithu.ToString());

            dgvHienThi.DataSource = hienthi;
            fix();

        }

        void XuLy(Entities.HDBanLe item)
        {
            if (item.NgayLap.Date >= batdau.Date && item.NgayLap.Date <= ketthuc)
            {
                sotiendathu += double.Parse(item.TongTienThanhToan);
                tongtien += double.Parse(item.TongTienThanhToan);

                Entities.BCBanHangTheoHoaDon tem = new Entities.BCBanHangTheoHoaDon();
                tem.KhachHang = "Bán lẻ";
                tem.MaHD = item.MaHDBanLe;
                tem.LoaiHoaDon = "Bán lẻ";
                tem.SoTienDaThu = double.Parse(item.TongTienThanhToan);
                tem.SoTienPhaiThu = 0;
                tem.STT = sotang + 1;
                tem.TongTien = double.Parse(Common.Utilities.FormatMoney(item.TongTienThanhToan));
                tem.GiaTriTT = Common.Utilities.FormatMoney(tem.TongTien.ToString());
                tem.GiaTriDT = Common.Utilities.FormatMoney(tem.SoTienDaThu.ToString());
                tem.GiaTriPT = Common.Utilities.FormatMoney(tem.SoTienPhaiThu.ToString());
                array1.Add(tem);
                sotang++;
            }
        }

        void XuLy(Entities.HDBanBuon item)
        {
            sotiendathu += double.Parse(item.ThanhToanNgay) + double.Parse(item.ThanhToanSauKhiLapPhieu);
            sotienphaithu += double.Parse(item.TongTienThanhToan) - (double.Parse(item.ThanhToanNgay) + double.Parse(item.ThanhToanSauKhiLapPhieu));
            tongtien += double.Parse(item.TongTienThanhToan);
            thue += double.Parse(item.ThueGTGT);
            chietkhau += double.Parse(item.ChietKhau);

            Entities.BCBanHangTheoHoaDon tem = new Entities.BCBanHangTheoHoaDon();
            tem.KhachHang = "Bán buôn";
            tem.MaHD = item.MaHDBanBuon;
            tem.LoaiHoaDon = "Bán buôn";
            tem.SoTienDaThu = double.Parse(item.ThanhToanNgay) + double.Parse(item.ThanhToanSauKhiLapPhieu);
            tem.SoTienPhaiThu = double.Parse(item.TongTienThanhToan) - (double.Parse(item.ThanhToanNgay) + double.Parse(item.ThanhToanSauKhiLapPhieu));
            tem.STT = sotang + 1;
            tem.TongTien = double.Parse(Common.Utilities.FormatMoney(item.TongTienThanhToan));
            tem.GiaTriTT = Common.Utilities.FormatMoney(tem.TongTien.ToString());
            tem.GiaTriDT = Common.Utilities.FormatMoney(tem.SoTienDaThu.ToString());
            tem.GiaTriPT = Common.Utilities.FormatMoney(tem.SoTienPhaiThu.ToString());
            array1.Add(tem);
            sotang++;
        }
        #endregion

        #region bán lẻ
        void XuLyBanLe()
        {
            sotiendathu = sotienphaithu = tongtien = 0;
            sotang = 0;
            array = new ArrayList();
            array1 = new ArrayList();
            foreach (Entities.HDBanLe item in hdbanle)
            {
                if (cbbKho.SelectedIndex == khohang.Length)
                {
                    if (item.NgayLap.Date >= batdau.Date && item.NgayLap.Date <= ketthuc)
                    {
                        XuLy(item);
                    }
                }
                else
                {
                    if (item.MaKho == makho && item.NgayLap.Date >= batdau.Date && item.NgayLap.Date <= ketthuc)
                    {
                        XuLy(item);
                    }
                }
            }
            hienthi = new Entities.BCBanHangTheoHoaDon[array1.Count + 1];
            if (array1.Count != 0)
            {
                sotang = 0;
                Entities.BCBanHangTheoHoaDon[] te = (Entities.BCBanHangTheoHoaDon[])array1.ToArray(typeof(Entities.BCBanHangTheoHoaDon));
                foreach (Entities.BCBanHangTheoHoaDon item in te)
                {
                    hienthi[sotang] = item;
                    sotang++;
                }
            }
            hienthi[hienthi.Length - 1] = new Entities.BCBanHangTheoHoaDon();
            hienthi[hienthi.Length - 1].LoaiHoaDon = "Tổng tiền";
            hienthi[hienthi.Length - 1].GiaTriTT = Common.Utilities.FormatMoney(tongtien.ToString());
            hienthi[hienthi.Length - 1].GiaTriDT = Common.Utilities.FormatMoney(sotiendathu.ToString());
            hienthi[hienthi.Length - 1].GiaTriPT = Common.Utilities.FormatMoney(sotienphaithu.ToString());


            dgvHienThi.DataSource = hienthi;
            fix();
        }
        #endregion

        #region bán buôn
        void XuLyBanBuon()
        {
            sotiendathu = sotienphaithu = tongtien = 0;
            sotang = 0;
            array = new ArrayList();
            array1 = new ArrayList();

            foreach (Entities.HDBanBuon item in hdbanbuon)
            {
                if (cbbKho.SelectedIndex == khohang.Length)
                {
                    if (item.NgayLap.Date >= batdau.Date && item.NgayLap.Date <= ketthuc)
                    {
                        XuLy(item);
                    }
                }
                else
                {
                    if (item.MaKho == makho && item.NgayLap.Date >= batdau.Date && item.NgayLap.Date <= ketthuc)
                    {
                        XuLy(item);
                    }
                }
            }

            hienthi = new Entities.BCBanHangTheoHoaDon[array1.Count + 1];
            if (array1.Count != 0)
            {
                sotang = 0;
                Entities.BCBanHangTheoHoaDon[] te = (Entities.BCBanHangTheoHoaDon[])array1.ToArray(typeof(Entities.BCBanHangTheoHoaDon));
                foreach (Entities.BCBanHangTheoHoaDon item in te)
                {
                    hienthi[sotang] = item;
                    sotang++;
                }
            }
            hienthi[hienthi.Length - 1] = new Entities.BCBanHangTheoHoaDon();
            hienthi[hienthi.Length - 1].LoaiHoaDon = "Tổng tiền";
            hienthi[hienthi.Length - 1].GiaTriTT = Common.Utilities.FormatMoney(tongtien.ToString());
            hienthi[hienthi.Length - 1].GiaTriDT = Common.Utilities.FormatMoney(sotiendathu.ToString());
            hienthi[hienthi.Length - 1].GiaTriPT = Common.Utilities.FormatMoney(sotienphaithu.ToString());

            dgvHienThi.DataSource = hienthi;
            fix();
        }
        #endregion

        #region Lọc điều kiện
        void LocDieuKien()
        {
            try
            {
                GUI.LocDieuKien.frmLocDieuKien frm = new LocDieuKien.frmLocDieuKien();
                frm.ShowDialog();
                if (GUI.LocDieuKien.frmLocDieuKien.kiemtra)
                {
                    DateTime truoc1;
                    DateTime sau1;
                    try
                    {
                        truoc1 = DateTime.Parse(GUI.LocDieuKien.frmLocDieuKien.truoc);
                        sau1 = DateTime.Parse(GUI.LocDieuKien.frmLocDieuKien.sau);
                    }
                    catch
                    {
                        return;
                    }
                    batdau = truoc1;
                    ketthuc = sau1;
                    XuLy();
                    GUI.LocDieuKien.frmLocDieuKien.kiemtra = false;
                }
            }
            catch
            {
            }
        }
        #endregion

        #region Word
        void Word()
        {
            if (hienthi.Length - 1 == 0)
            {
                return;
            }
            try
            {
                savefile.Filter = "Word |*.doc"; savefile.FileName = "";
                if (savefile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Entities.BCBanHangTheoHoaDon[] temp = new Entities.BCBanHangTheoHoaDon[hienthi.Length - 1];
                    int sotang = 0;
                    foreach (Entities.BCBanHangTheoHoaDon item in hienthi)
                    {
                        if (item.MaHD != null)
                        {
                            temp[sotang] = item;
                            sotang++;
                        }
                    }
                    BaoCao.frmBaoCaorpt frm = new frmBaoCaorpt(temp, tenkho, loaihoadon, batdau, ketthuc, savefile.FileName, Common.Constants.Word);
                }
            }
            catch
            {
            }
        }
        #endregion

        #region Excel
        void Excel()
        {
            if (hienthi.Length - 1 == 0)
            {
                return;
            }
            try
            {
                savefile.Filter = "Excel |*.xls"; savefile.FileName = "";
                if (savefile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Entities.BCBanHangTheoHoaDon[] temp = new Entities.BCBanHangTheoHoaDon[hienthi.Length - 1];
                    int sotang = 0;
                    foreach (Entities.BCBanHangTheoHoaDon item in hienthi)
                    {
                        if (item.MaHD != null)
                        {
                            temp[sotang] = item;
                            sotang++;
                        }
                    }
                    BaoCao.frmBaoCaorpt frm = new frmBaoCaorpt(temp, tenkho, loaihoadon, batdau, ketthuc, savefile.FileName, Common.Constants.Excel);
                }
            }
            catch
            {
            }
        }
        #endregion

        #region PDF
        void PDF()
        {
            if (hienthi.Length - 1 == 0)
            {
                return;
            }
            try
            {
                savefile.Filter = "PDF |*.pdf"; savefile.FileName = "";
                if (savefile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Entities.BCBanHangTheoHoaDon[] temp = new Entities.BCBanHangTheoHoaDon[hienthi.Length - 1];
                    int sotang = 0;
                    foreach (Entities.BCBanHangTheoHoaDon item in hienthi)
                    {
                        if (item.MaHD != null)
                        {
                            temp[sotang] = item;
                            sotang++;
                        }
                    }
                    BaoCao.frmBaoCaorpt frm = new frmBaoCaorpt(temp, tenkho, loaihoadon, batdau, ketthuc, savefile.FileName, Common.Constants.PDF);
                }
            }
            catch
            {
            }
        }
        #endregion

        #region Thoát
        void Thoat()
        {
            try
            {
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)             
                {
                    checkClose = true;
                    this.Close();
                }
            }
            catch
            {
            }
        }
        #endregion

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }

        private void cbbLoai_SelectedIndexChanged(object sender, EventArgs e)
        {
            loaihoadon = cbbLoai.Text;
            XuLy();
        }

        private void cbbKho_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbKho.SelectedIndex < 0)
                return;
            makho = cbbKho.SelectedValue.ToString();
            tenkho = cbbKho.Text;
            XuLy();
        }

        private void frmBanHangTheoHoaDon_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (!checkClose)
                    if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) ==  System.Windows.Forms.DialogResult.Yes)
                    {
                        e.Cancel = checkClose;
                    }
                    else
                        e.Cancel = !checkClose;
            }
            catch
            {
            }
        }

        private void dgvHienThi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                i = e.RowIndex;
                save = ((Entities.BCBanHangTheoHoaDon[])dgvHienThi.DataSource)[i];
            }
        }

        private void dgvHienThi_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            In();
        }

        private void frmBanHangTheoHoaDon_Load(object sender, EventArgs e)
        {
                dgvHienThi.DataSource = new Entities.BCBanHangTheoHoaDon[0];
                fix();

        }

        private void fix()
        {
            try
            {
                for (int i = 0; i < dgvHienThi.ColumnCount; i++)
                {
                    dgvHienThi.Columns[i].Visible = false;
                }
                dgvHienThi.ReadOnly = true;
                dgvHienThi.Columns["MaHD"].Visible = true;
                dgvHienThi.Columns["LoaiHoaDon"].Visible = true;
                dgvHienThi.Columns["GiaTriTT"].Visible = true;
                dgvHienThi.Columns["GiaTriDT"].Visible = true;
                dgvHienThi.Columns["GiaTriPT"].Visible = true;
                dgvHienThi.Columns["MaHD"].HeaderText = "Mã Hóa Đơn";
                dgvHienThi.Columns["LoaiHoaDon"].HeaderText = "Loại Hòa Đơn";
                dgvHienThi.Columns["GiaTriTT"].HeaderText = "Thành Tiền";
                dgvHienThi.Columns["GiaTriDT"].HeaderText = "Số Tiền Đã Thu";
                dgvHienThi.Columns["GiaTriPT"].HeaderText = "Số Tiền Phải Thu";
                dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                dgvHienThi.AllowUserToAddRows = false;
                dgvHienThi.AllowUserToDeleteRows = false;
                dgvHienThi.AllowUserToResizeRows = false;
                dgvHienThi.RowHeadersVisible = false;

            }
            catch
            {
            }
            
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            In();
        }

        private void btnLocDieuKien_Click(object sender, EventArgs e)
        {
            LocDieuKien();
        }

        private void btnWord_Click(object sender, EventArgs e)
        {
            Word();
        }

        private void btnExcel_Click(object sender, EventArgs e)
        {
            Excel();
        }

        private void btnPDF_Click(object sender, EventArgs e)
        {
            PDF();
        }
    }
}
